﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;

namespace CuocChienQueNha.Class.File
{
    public class LevelFile
    {
        public List<LevelStruct> levels { get; set; }
        IsolatedStorageFile storage;
        public LevelFile()
        {
            levels = new List<LevelStruct>();
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("level.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("level.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("1;1;1;2");
                    writer.WriteLine("2;1;1;1");
                    writer.WriteLine("3;1;1;1");

                    writer.WriteLine("4;2;1;3");
                    writer.WriteLine("5;2;1;3");
                    writer.WriteLine("6;2;1;1");
                    writer.WriteLine("7;2;1;2");
                    writer.WriteLine("8;2;1;2");
                    writer.WriteLine("9;2;1;2");

                    writer.WriteLine("10;3;1;0");
                    writer.WriteLine("11;3;0;0");
                    writer.WriteLine("12;3;0;0");
                    writer.WriteLine("13;3;0;0");
                    writer.WriteLine("14;3;0;0");
                    writer.WriteLine("15;3;0;0");

                    writer.WriteLine("16;4;0;0");
                    writer.WriteLine("17;4;0;0");
                    writer.WriteLine("18;4;0;0");
                    writer.WriteLine("19;4;0;0");
                    writer.WriteLine("20;4;0;0");
                    writer.WriteLine("21;4;0;0");

                    writer.WriteLine("22;4;0;0");
                    writer.WriteLine("23;4;0;0");
                    writer.WriteLine("24;4;0;0");

                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("level.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    levels.Add(new LevelStruct(r.ReadLine()));
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
            storage.Dispose();
        }
    }
    public class LevelStruct
    {
        public int level { get; set; }
        public int bando { get; set; }
        public bool isOpen { get; set; }
        public int star { get; set; }
        public LevelStruct(string t)
        {
            string[] temp = t.Split(';');
            if (temp.Length == 4)
            {
                level = Convert.ToInt16(temp[0]);
                bando = Convert.ToInt16(temp[1]);
                isOpen = (Convert.ToInt16(temp[2]) == 0 ? false : true);
                star = Convert.ToInt16(temp[3]);
            }
        }
    }
}
